<template>
<div><el-breadcrumb separator="/">
  <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
  <el-breadcrumb-item>供应商管理</el-breadcrumb-item>
  <el-breadcrumb-item>供应商列表</el-breadcrumb-item>

</el-breadcrumb >
<el-card>
  <el-row :gutter="20">
    <el-col :span="7">
    <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getUserlist">
    <el-button slot="append" icon="el-icon-search" @click="getUserlist" ></el-button>
  </el-input>
 </el-col>
    <el-col :span="4"><el-button type="primary" @click="dialogVisible=true">添加供应商</el-button></el-col>
  </el-row>
  <el-table :data="userlist" border stripe>
    <el-table-column type="index" label="序号"></el-table-column>
    <el-table-column prop="username" label="供应商名称"></el-table-column>
<!--    <el-table-column prop="email" label="邮箱"></el-table-column>-->
    <el-table-column prop="mobile" label="电话"></el-table-column>
    <el-table-column prop="role_name" label="等级"></el-table-column>
    <el-table-column prop="mg_state" label="合作状态" >
      <template slot-scope="scope">
      <el-switch v-model="scope.row.mg_state" @change="userStateChange(scope.row)"></el-switch></template>
    </el-table-column>
    <el-table-column  label="操作" >
      <template slot-scope="scope">
        <el-button type="primary" icon="el-icon-edit" circle @click="rectify_user(scope.row.id)"></el-button>
        <el-button type="danger" icon="el-icon-delete" circle @click="delete_user(scope.row.id)"></el-button>
         <el-tooltip class="item" effect="dark" content="分配角色" placement="top">
      <el-button type="warning" icon="el-icon-setting" circle ></el-button>
    </el-tooltip>
</template>
    </el-table-column>

  </el-table>
<!--  分页-->
   <div class="block">

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[1, 2, 5, 10]"
      :page-size="100"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
   </el-card>
<!--  这是dialog对话框1 添加用户的-->
  <el-dialog
  title="提示"
  :visible.sync="dialogVisible"
  width="50%"
   @close="formclose">
<!--    这是表单-->
  <el-form :model="addruleForm" :rules="addUserrules" ref="addruleForm" label-width="110px" status-icon>
  <el-form-item label="供应商名称" prop="username">
    <el-input v-model="addruleForm.username"></el-input>
  </el-form-item>
  </el-form>
    <el-form :model="addruleForm" :rules="addUserrules" ref="addruleForm" label-width="110px" >
  <el-form-item label="密码" prop="password">
    <el-input v-model="addruleForm.password"></el-input>
  </el-form-item>
  </el-form>
    <el-form :model="addruleForm" :rules="addUserrules" ref="addruleForm" label-width="110px" >
  <el-form-item label="负责人手机号" prop="mobile">
    <el-input v-model="addruleForm.mobile"></el-input>
  </el-form-item>
  </el-form>
    <el-form :model="addruleForm" :rules="addUserrules" ref="addruleForm" label-width="110px" >
  <el-form-item label="企业邮箱" prop="email">
    <el-input v-model="addruleForm.email"></el-input>
  </el-form-item>
  </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary"  @click="dialogsure">确 定</el-button>
  </span>
</el-dialog>
<!--  这是修改供应商的对话框-->
<el-dialog
  title="修改供应商"
  :visible.sync="dialogVisible2"
  width="50%" @click="formclose2"
  >
  <el-form :model="search_info" :rules="addUserrules" ref="search_info" label-width="110px" status-icon>
  <el-form-item label="供应商名称" >
    <el-input v-model="search_info.username" disabled></el-input>
  </el-form-item>
  </el-form>
    <el-form :model="search_info" :rules="seachrules" ref="search_info" label-width="110px" >
  <el-form-item label="负责人手机号" prop="mobile">
    <el-input v-model="search_info.mobile"></el-input>
  </el-form-item>
  </el-form>
    <el-form :model="search_info" :rules="seachrules" ref="ssearch_info" label-width="110px" >
  <el-form-item label="企业邮箱" prop="email">
    <el-input v-model="search_info.email"></el-input>
  </el-form-item>
  </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible2 = false">取 消</el-button>
    <el-button type="primary"  @click="dialogsure2">确 定</el-button>
  </span>
</el-dialog>
</div>
</template>

<script>
import {getusers} from "../../networks/get_users";
import {rectify_state} from "../../networks/rectify_state";
import {add_users} from "../../networks/add_users";
import {search_users} from "../../networks/search_user";
import {rectify_users} from "../../networks/rectify_users";
import {deleteUsers} from "../../networks/delete_users";

export default {
name: "Users",
  data()
  {
  // var checkemail=(rule,value,cb)=>{
  //   const reg = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/
  //   if(reg.test(value)){
  //     return cb()
  //   }
  //   cb(new Error('邮箱不合法'))
  // }
     let checkemail = (rule, value, callback) => {
      // 验证邮箱正则
      const reg = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/
      if (reg.test(value)) return callback()

      callback(new Error('请输入合法邮箱'))
    }
     let checkMobile = (rule, value, callback) => {
      const reg = /^1[3|4|5|7|8][0-9]{9}$/
      if (reg.test(value)) return callback()
      callback(new Error('请输入合法手机号'))
    }
  return{

    queryInfo:{
      query:'',
      pagenum:1,
      pagesize:2
    },
    userlist:[],
    total:0,
    dialogVisible:false,
    dialogVisible2:false,
    addruleForm:{
      username:'',
      password:'',
      email:'',
      mobile:'',

    },
   search_info:{},
    addUserrules:{
      username:[
           { required: true, message: '请输入供应商名称', trigger: 'blur' },
            { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
      ],
      password:[
           { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
      ],
       mobile:[
           { required: true, message: '请输入联系人手机号', trigger: 'blur' },
         {validator:checkMobile,trigger: 'blur'}
      ],
       email:[
           { required:true, message: '请输入供应商邮箱', trigger: 'blur' },
                      { validator:checkemail, trigger: 'blur' }


      ]
    },
    seachrules:{
        mobile:[
           { required: true, message: '请输入联系人手机号', trigger: 'blur' },
         {validator:checkMobile,trigger: 'blur'}
      ],
       email:[
           { required:true, message: '请输入供应商邮箱', trigger: 'blur' },
                      { validator:checkemail, trigger: 'blur' }


      ]
    }
  }
  },
  methods:{
  formclose(){
    this.$refs.addruleForm.resetFields()

  }, formclose2(){
    this.$refs.searchruleinfo.resetFields()

  },
    dialogsure(){
    this.$refs.addruleForm.validate(
        valid=>{
          if (!valid){return this.$message.error('信息有误')}
          add_users(this.addruleForm.username,this.addruleForm.password,this.addruleForm.email,this.addruleForm.mobile).then(res=>{
            if(res.meta.status!==201){
              this.$message.error('添加用户失败')
            }
            this.$message.success('添加用户成功')
            this.dialogVisible=false
            this.getUserlist()

          })
        }
    )

    },
    dialogsure2(){
    this.$refs.search_info.validate(
        valid=>{
          if (!valid){return this.$message.error('信息有误')}

          rectify_users(this.search_info.id,this.search_info).then(res=>{
            if(res.meta.status!==200){this.$message.error('更新信息失败')}
            this.$message.success('修改信息成功')
            this.dialogVisible2=false
          })
        }
    )

    },
  getUserlist(){
    getusers(this.queryInfo).then(res=>{

    if (res.meta.status!==200){return this.$message.error('请求数据失败，请检查网络')}
    this.userlist=res.data.users
      this.total=res.data.total

        }
    )

  },
    // 监听pagesize发生改变事件
      handleSizeChange(newSize){
        this.queryInfo.pagesize=newSize
        this.getUserlist()
      },
    // 监听 页码值发生改变事件
    handleCurrentChange(newPage){
      this.queryInfo.pagenum=newPage
      this.getUserlist()
    },
    userStateChange(state){

      rectify_state(state.id,state.mg_state).then(
          res=>{
           if(res.meta.status!==200) {
             this.$message.error('状态修改失败')
             state.mg_state=!state.mg_state
           }
           this.$message.success('状态修改成功')
          }
      )
    },
    rectify_user(state){
    this.dialogVisible2=true
      search_users(state).then(
          res=>{
            if (res.meta.status!==200){return this.$message.error('找不到用户')}
          this.search_info=res.data}
      )
    },
    delete_user(id) {
      this.$confirm('此操作将永久删除该供应商, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteUsers(id).then(res=>{
         if(res.meta.status!==200){this.$message.error('删除失败')}
         this.getUserlist()})
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    }
  },
  created() {
  this.getUserlist()
  }
}
</script>

<style scoped>
.el-table{
  margin-top: 20px;

}
.el-form-item__content > div:nth-child(1){
  width: 400px;
}

</style>